package com.fingard.dsp.bank.directbank.psbc11;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 邮保通单笔实时代扣
 * 
 * 参考文档:中国邮政储蓄银行代理保险大集中系统-实时代收交易接口规范V1.1.doc
 * 
 * @author Cruise
 * @date 2019/4/18
 */
public class PSBC11RealTime extends PSBC11Base implements IRealTime {
	/** 
	 * 参考接口:2.1 扣款请求报文(816090)
	 */
	@Override
	public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
		
		try {
			PSBC11Header tmpBankHeader = new PSBC11Header();
			tmpBankHeader.transCode = PSBC11TransCode.singleRec_9108_910801;
			tmpBankHeader.reqSeqID = p_req9108.singleDetail.askNumber;
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
            xmlWriter.autoFormat = false;
            writeCommonHeader(xmlWriter, tmpBankHeader);
            
            xmlWriter.writeStartElement("CUST");
            xmlWriter.writeElementString("PAYANM", p_req9108.singleDetail.payName);
            xmlWriter.writeElementString("PAYACC", p_req9108.singleDetail.payAct);
            xmlWriter.writeElementString("IDEN", p_req9108.singleDetail.certNum);
            xmlWriter.writeElementString("AMT", StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount));
            xmlWriter.writeEndDocument();
			
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpBankHeader.transCode);
            
            if(tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					tmpRetDetail.respCode = "0";
					tmpRetDetail.respInfo = tmpBkReader.getFirstText("/RETURN/MAIN/ERR_INFO");
					
					String tmpRespCode = tmpBkReader.getFirstText("/RETURN/MAIN/RESULTCODE");
					tmpRetDetail.transState = tmpRespCode;
					tmpRetDetail.payInfoCode = tmpRespCode;
					if ("ZZ".equals(tmpRespCode)) {
						tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/RETURN/MAIN/ERR_INFO");
					} 
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1"; 
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	/**
	 * 参考接口:4.1 业务状态查询请求报文(816092)
	 */
	@Override
	public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		tmpRetDetail.reqSeqID = p_req9144.askNumber;
		
		try {
			PSBC11Header tmpBankHeader = new PSBC11Header();
			tmpBankHeader.transCode = PSBC11TransCode.singleQryRec_9144_910801;
			tmpBankHeader.reqSeqID = getQryTransID();
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
            xmlWriter.autoFormat = false;
            writeCommonHeader(xmlWriter, tmpBankHeader);
            
            xmlWriter.writeStartElement("CUST");
            xmlWriter.writeElementString("ORGTRANDATE", Format.Date8Format.format(p_req9144.getBgnDateTime()));
            xmlWriter.writeElementString("ORGTRANSRNO", p_req9144.askNumber);
            xmlWriter.writeEndDocument();
			
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpBankHeader.transCode);
            
            if(tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					tmpRetDetail.respCode = "0";
					tmpRetDetail.respInfo = tmpBkReader.getFirstText("/RETURN/MAIN/ERR_INFO");
					
					// 业务处理状态:01-原交易扣款成功  02-原交易扣款取消成功  03-原交易失败 04-无原交易流水记录 05-原交易需要人工核查
					String status = tmpBkReader.getFirstText("/RETURN/MAIN/BUSISTAT");
					String tmpRespCode = tmpBkReader.getFirstText("/RETURN/MAIN/RESULTCODE");
					tmpRetDetail.transState = status;
					tmpRetDetail.payInfoCode = tmpRespCode;
					if ("ZZ".equals(tmpRespCode)) {
						tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/RETURN/MAIN/ERR_INFO");
					} 
					if ("04".equals(status)) {
						if (canBeNotExist(p_req9144, tmpRetDetail, 45)) {
							tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = "记录不存在!";
                            tmpRetDetail.transState = "E1302";
                            tmpRetDetail.payInfoCode = "E1302";
                            retAts.writeRetDetail(tmpRetDetail);
                            return retAts;
						}
					} 
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1"; 
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}
}
